/**
 * the file is after loading page ,draw the driver return from server on the map  
 */
(function(){
	var map, markerPassager, markerDriver, passagerWin, driverWin;
	window.initializeOtherElement(map);
	
})(jQuery);



/**
 * initialize map acccording to the given position
 * @param lat
 * @param lng
 */
function initializeMap() {
	var position = new google.maps.LatLng(window.customer.latitude, window.customer.longitude);
	var mapOptions = {
			zoom: 16,
			mapTypeId: google.maps.MapTypeId.ROADMAP,
			center: position,
			panControl : false,
			zoomControl : false,
			scaleControl : false,
			streetViewControl : false,
			overviewMapControl : false,
			mapTypeControl : false
	};
	map = new google.maps.Map(document.getElementById("mapContainer"), mapOptions);
	
}

/**
 * return a Marker on the given map according to the given position
 * @param latlng
 * @param map
 * @returns {google.maps.Marker}
 */

function initializeMarker(latlng, map) {
	marker = new google.maps.Marker({
		map: map,
		visible: true,
		draggable: true,
		animation: google.maps.Animation.DROP,
		position: latlng
	});
	return marker;
}

/**
 * initialize other elements like, markers, infowindows and so on
 */
window.initializeOtherElement = function(map) {
	initializeDriverInfo();
	initializeListener();
	driverLatLng = new google.maps.LatLng(window.backDriInfo.driverInfo.lat, window.backDriInfo.driverInfo.lng);
	markerDriver = initializeMarker(driverLatLng);
};

/**
 * initialize driver info in $("driverInfo")
 */
function initializeDriverInfo() {
	$("#driphoto").attr("src",window.backDriInfo.driverInfo.photo);
	$("#name").text(window.backDriInfo.driverInfo.driverName);
	$("#credit").text(window.backDriInfo.driverInfo.credit);
}

function initializeListener() {
	$('#acceptBtn').on('tap', function(){
		var invocationData = {
				adapter : 'stAdp',
				procedure : 'sendAcceptReq',
				parameters : [{'driverId' : window.backDriInfo.driverInfo.driverId, 'customerId': window.customer.id}]
		};
		busy.show();
		WL.Client.invokeProcedure(invocationData, {
			onSuccess : acceptSuccess,
			onFailure : acceptFailure
		});
	});
	$('#refuseBtn').on('tap', function(){
		var invocationData = {
				adapter : 'stAdp',
				procedure : 'sendRefuseReq',
				parameters : [{'driverId' : window.backDriInfo.driverInfo.driverId, 'customerId': window.customer.id}]
		};
		busy.show();
		WL.Client.invokeProcedure(invocationData, {
			onSuccess : refuseSuccess,
			onFailure : refuseFailure
		});
	});
	$('#microphoto').on('tap', controlRecord);
	
}

function acceptSuccess(result) {
	busy.hide();
	window.backDriInfo.driverInfo.lat = result.invocationResult.currentLat;
	window.backDriInfo.driverInfo.lng = result.invocationResult.currentLng;
	window.backDriInfo.distance = result.invocationResult.distance;
	window.backDriInfo.saveTime = result.invocationResult.saveTime;
	
	$.mobile.changePage("../view/real-time.html");
//	var distance = result.invocationResult.distance;
//	var saveTime = result.invocationResult.saveTime;
//	driverWin = new google.maps.InfoWindow({
//		content:"<p>distance:" + distance + "</p>" +
//				"<p>saveTime:" + saveTime + "</p>"
//	});
//	driverWin.open(map, markerDriver);
}

function acceptFailure(result) {
	alert("error");
}

function refuseSuccess(result) {
	busy.hide();
	window.page=0;
	$.mobile.changePage("../view/customer-main.html");
}

function refuseFailure(result) {
	busy.hide();
	window.page=0;
	alert("fail");
	$.mobile.changePage("../view/customer-main.html");
}

/**
 * after tap the mic start record or stop record
 */
function controlRecord() {
	alert("aaa");
	var currentStatus = $('#microphoto').data('status');
	alert($('#microphoto').data('status'));
	if(currentStatus == "stop") {
		startRecord();
		$('#microphoto').data('status', "running");
		alert($('#microphoto').data('status'));
	} else if(currentStatus == "running") {
		stopRecord();
		$('#microphoto').data('status', "stop");
		alert($('#microphoto').data('status'));
	}
}
var mediaRec, recTime, medial, media1Timer;
function startRecord() {
	mediaRec = null;
    recTime = 0;
    if (media1 != null) {
        stopAudio();
        media1 = null;
    }
    
    function record() {
		// Record audio
        mediaRec.startRecord();

        // Stop recording after 10 sec
        recTime = 0;
        var recInterval = setInterval(function() {
            recTime = recTime + 1;
            setAudioPosition(recTime+" sec");
            if (recTime >= 10) {
                clearInterval(recInterval);
                if (mediaRec.stopAudioRecord){
                    mediaRec.stopAudioRecord();
                } else {
                    mediaRec.stopRecord();
                }
                console.log("recordAudio(): stop");
                navigator.notification.beep(1);
            }
        }, 1000);
	}
}

function stopRecord() {
	
}

/**
 * Stop audio
 */
function stopAudio(shouldAlert) {
 	alert("stopAudio()");
    if(mediaStatus == Media.MEDIA_STOPPED) {
    	if(shouldAlert) {
    		alert('Audio is already stopped.');
    	}
    	return;
    }
    if (media1) {
        media1.stop();
    }
    clearInterval(media1Timer);
    media1Timer = null;
}
